package com.ruoyi.web.controller.task;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.TaskReviewDto;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.task.domain.Task;
import com.ruoyi.task.service.ITaskService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务Controller
 *
 * @author KevenPotter
 * @date 2024-04-28 16:52:51
 */
@RestController
@RequestMapping("/task/task")
@AllArgsConstructor
public class TaskController extends BaseController {

    private final ITaskService taskService;
    private final TokenService tokenService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(Task task) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<Task> list = taskService.getList(task);
        list.forEach(item -> taskService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 查询精选任务列表
     *
     * @param task 任务对象
     * @return 返回精选任务列表
     * @author KevenPotter
     * @date 2024-04-30 09:15:51
     */
    @PreAuthorize("@ss.hasPermi('task:taskFeatured:list')")
    @GetMapping("/listFeatured")
    public TableDataInfo listFeatured(Task task) {
        task.setType("2");
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<Task> list = taskService.getList(task);
        list.forEach(item -> taskService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task) {
        List<Task> list = taskService.getList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Task task = taskService.getById(id);
        task = taskService.filterOutbound(task, loginUser);
        return success(task);
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('task:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        task = taskService.filterInbound(task, loginUser);
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('task:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        task = taskService.filterInbound(task, loginUser);
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('task:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(taskService.deleteTaskByIds(ids));
    }

    /**
     * 审核任务
     *
     * @param taskReviewDto 任务审核数据传输
     * @return 返回是否审核成功
     * @author KevenPotter
     * @date 2024-04-29 09:44:16
     */
    @PreAuthorize("@ss.hasPermi('task:task:review')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody TaskReviewDto taskReviewDto) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskReviewDto.setReviewUserId(loginUser.getUserId());
        return toAjax(taskService.review(taskReviewDto));
    }

    /**
     * 标记精选操作
     *
     * @param taskId 任务编号
     * @return 返回标记精选是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:03:16
     */
    @PreAuthorize("@ss.hasPermi('task:task:markFeatured')")
    @PutMapping("/markFeatured/{taskId}")
    public AjaxResult markFeatured(@PathVariable("taskId") Long taskId) {
        return toAjax(taskService.markFeatured(taskId));
    }

    /**
     * 取消精选操作
     *
     * @param taskId 任务编号
     * @return 返回取消精选是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:03:51
     */
    @PreAuthorize("@ss.hasPermi('task:task:cancelFeatured')")
    @PutMapping("/cancelFeatured/{taskId}")
    public AjaxResult cancelFeatured(@PathVariable("taskId") Long taskId) {
        return toAjax(taskService.cancelFeatured(taskId));
    }

    /**
     * 标记置顶操作
     *
     * @param taskId 任务编号
     * @return 返回标记置顶是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:44:27
     */
    @PreAuthorize("@ss.hasPermi('task:task:markTop')")
    @PutMapping("/markTop/{taskId}")
    public AjaxResult markTop(@PathVariable("taskId") Long taskId) {
        return toAjax(taskService.markTop(taskId));
    }

    /**
     * 取消置顶操作
     *
     * @param taskId 任务编号
     * @return 返回取消置顶是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:44:45
     */
    @PreAuthorize("@ss.hasPermi('task:task:cancelTop')")
    @PutMapping("/cancelTop/{taskId}")
    public AjaxResult cancelTop(@PathVariable("taskId") Long taskId) {
        return toAjax(taskService.cancelTop(taskId));
    }

    /**
     * 查询任务字典
     *
     * @return 返回任务字典
     * @author KevenPotter
     * @date 2024-04-29 15:43:19
     */
    @GetMapping("/getTaskDicts")
    public AjaxResult getTaskDicts() {
        List<Task> allList = taskService.getAllList();
        List<SysDictData> taskDictList = new ArrayList<>();
        allList.forEach(item -> {
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictLabel(item.getTitle());
            sysDictData.setDictValue(String.valueOf(item.getId()));
            taskDictList.add(sysDictData);
        });
        return AjaxResult.success(taskDictList);
    }

}
